import { Component, OnInit } from '@angular/core';
import { ActivatedRoute,Router } from '@angular/router';
import { AuthService } from '../common/services/auth.service';
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  public user = { userName: '', password: '' };
  validateForm: FormGroup;

  constructor(public router: Router, 
    public activatedRoute: ActivatedRoute, 
    private authService: AuthService,
    private fb: FormBuilder) {}
  //constructor(public router: Router){}
  ngOnInit() {
    this.authService.logout();
    this.validateForm = this.fb.group({
      userName: [ null, [ Validators.required ] ],
      password: [ null, [ Validators.required ] ],
      remember: [ true ]
    });
  }

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[ i ].markAsDirty();
      this.validateForm.controls[ i ].updateValueAndValidity();
    }
  }

  login() {
    this.authService.loggin().subscribe(() => {
      const redirect = this.authService.redirectUrl ? this.authService.redirectUrl : '/workspace';
      this.router.navigate([redirect]);
    });
    //this.router.navigateByUrl('workspace');
  }

  forgetPwd() {}
}
